<template>
<div>
  <el-breadcrumb separator="/">
    <el-breadcrumb-item :to="{ path: '/Home' }">首页</el-breadcrumb-item>
    <el-breadcrumb-item>管理员</el-breadcrumb-item>
  </el-breadcrumb>
  <el-card>
    <el-row :gutter="20">
      <el-col :span="8">
        <el-input placeholder="请输入管理员姓名" clearable v-model="name" @clear="searchManager">
          <el-button slot="append" icon="el-icon-search" @click="searchManager"></el-button>
        </el-input>
      </el-col>
      <el-col :span="4">
        <el-button type="primary" @click="showAddDialog">添加管理员</el-button>
      </el-col>
    </el-row>
    <el-table :data="managerList" border stripe>
      <el-table-column type="index"></el-table-column>
      <el-table-column label="店员姓名" prop="username"></el-table-column>
      <el-table-column label="电话" prop="phone"></el-table-column>

      <el-table-column label="角色">
        <template slot-scope="scope">
<!--          {{scope.row.roles}}-->
          {{getRoleNames(scope.row.roles)}}
        </template>
      </el-table-column>
      <el-table-column label="状态" >
        <template slot-scope="sc">{{sc.row.status==1?'正常':'冻结'}}</template>
      </el-table-column>
      <el-table-column label="操作">
        <template slot-scope="scope">
          <el-button type="primary" size="mini" @click="showEditDialog(scope.row)">修改</el-button>
          <el-button type="danger" size="mini" @click="removeManagerById(scope.row.id)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="pageNum"
        :page-sizes="[3, 6 , 12, 24]"
        :page-size="pageSize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
    ></el-pagination>
  </el-card>





  <!----------------------------------------------------添加用戶-------------------------------------------------------------->
  <el-dialog title="添加用戶" :visible.sync="addDialogVisible" width="50%" @close="addManagerFormClose">
    <el-form
        :model="addManagerForm"
        :rules="addManagerFormRules"
        ref="addManagerFormRef"
        label-width="100px"
        class="demo-ruleForm"
    >
      <el-form-item label="管理员名" prop="username">
        <el-input v-model="addManagerForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="addManagerForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="addManagerForm.cellphone"></el-input>
      </el-form-item>



      <el-form-item label="角色" prop="role">
        <el-select v-model="roleIds" multiple placeholder="请选择角色">
          <el-option :label="item.roleName" :value="item.id" v-for="item in roleList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否锁定" prop="status">
        <el-select v-model="addManagerForm.status" placeholder="请选择是否锁定账户">
          <el-option label="否" :value="0"></el-option>
          <el-option label="是" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="addManager">确 定</el-button>
        <el-button @click="cancelAdd">取 消</el-button>
      </span>
  </el-dialog>






  <!------------------------------------------------------修改用戶------------------------------------------------------->
  <el-dialog
      title="修改用戶"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editManagerFormClose"
  >
    <el-form
        :model="editManagerForm"
        :rules="editManagerFormRules"
        ref="editManagerFormRef"
        label-width="100px"
        class="demo-ruleForm"
    >
      <el-form-item label="管理员名" prop="username">
        <el-input v-model="editManagerForm.name"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="editManagerForm.password" type="password"></el-input>
      </el-form-item>
      <el-form-item label="手机" prop="phone">
        <el-input v-model="editManagerForm.cellphone"></el-input>
      </el-form-item>



      <el-form-item label="角色" prop="role">
        <el-select v-model="roleIds" multiple placeholder="请选择角色">
          <el-option :label="item.roleName" :value="item.id" v-for="item in roleList" :key="item.id"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="是否锁定" prop="status">
        <el-select v-model="editManagerForm.status" placeholder="请选择是否锁定账户">
          <el-option label="否" :value="0"></el-option>
          <el-option label="是" :value="1"></el-option>
        </el-select>
      </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="editManager">确 定</el-button>
        <el-button @click="editDialogVisible = false">取 消</el-button>
      </span>
  </el-dialog>
</div>
</template>

<script>
export default {
  // eslint-disable-next-line vue/multi-word-component-names
  name: "Manager",
  data(){
    return{
      pageNum: 1,
      pageSize: 3,
      total: 0,
      addDialogVisible:false,
      editDialogVisible:false,
      managerList:[],
      roleIds:[],
      roleList:[],
      name:'',
      addManagerForm: {
        name: "",
        password: "",
        status: "",
        roleIds: [],
        email: "",
        cellphone: "",

      },
      editManagerForm: {
        id: -1,
        name: "",
        password: "",
        status: 0,
        roleIds: [],
        email: "",
        cellphone: "",

      },
      editManagerFormRules: {
        name: [
          { required: true, message: "請輸入管理员名", trigger: "blur" },
          { min: 2, max: 8, message: "管理员名在2-8個字符之間", trigger: "blur" }
        ],
        password: [
          { required: true, message: "請輸入密码", trigger: "blur" },
          { min: 2, max: 8, message: "密码在2-8個字符之間", trigger: "blur" }
        ],
        cellphone: [
          { required: true, message: "請輸入电话号", trigger: "blur" },
          {pattern:/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/, message:"请输入符合格式的电话号" ,trigger: "blur"}
        ],

        status: [{ required: true, message: "请选择账号状态", trigger: "change" }],
        email:[{required: true, message: "請輸入邮箱", trigger: "blur"},
          {pattern:/^([A-Za-z0-9_\-.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/, message:"请输入符合格式的邮箱" ,trigger: "blur"}],

      },
      addManagerFormRules: {
        name: [
          { required: true, message: "請輸入管理员名", trigger: "blur" },
          { min: 2, max: 8, message: "管理员名在2-8個字符之間", trigger: "blur" }
        ],
        password: [
          { required: true, message: "請輸入密码", trigger: "blur" },
          { min: 2, max: 8, message: "密码在2-8個字符之間", trigger: "blur" }
        ],
        cellphone: [
          { required: true, message: "請輸入电话号", trigger: "blur" },
          {pattern:/^((13[0-9])|(14[5|7])|(15([0-3]|[5-9]))|(18[0,5-9]))\d{8}$/, message:"请输入符合格式的电话号" ,trigger: "blur"}
        ],
        deptName: [{ required: true, message: '請选择部门', trigger: 'change' }],
        status: [{ required: true, message: "请选择账号状态", trigger: "change" }],
        email:[{required: true, message: "請輸入邮箱", trigger: "blur"},
          {pattern:/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/, message:"请输入符合格式的邮箱" ,trigger: "blur"}],

      },
    };

  },
  methods:{
    handleSizeChange(newSize) {
      this.pageSize = newSize;
      this.pageNum = 1;
      this.initManager();
    },
    handleCurrentChange(newPage) {
      this.pageNum = newPage;
      this.initManager();
    },
    searchManager(){
      this.initManager();
    },
    showAddDialog(){
      this.addDialogVisible = true;
      // this.addManagerForm = row;
    },



    //--------------------------------------------------------初始化查询 所有管理员----------------------------------------------
    initManager(){
      let url = "/manager/list";
      this.$axios.get(url,{
        params:{
          now:this.pageNum,
          size:this.pageSize,
          name:this.name
        }
      }).then(res=>{
        if(res.data.code == 200){
          this.managerList = res.data.data.list ;
          console.log("=============================")
          console.log(this.managerList);
          console.log("=============================")
          this.pageSize = res.data.data.pageSize;
          this.pageNum = res.data.data.pageNum ;
          this.total = res.data.data.total ;
        }else {
          this.$message({
            type:'error',
            message:res.data.msg,
            duration:1000
          })
        }
      })
    },

    getRoleNames(list){
      let info = '';
      if (list != null && list.length >0){
        for (let r of list){
          info += r.roleName +","
        }
      }
      return info.substring(0,info.length-1) ;
    },




    //----------------------------------------------------------添加管理员---------------------------------------------------
    addManager() {
      this.$refs.addManagerFormRef.validate(valid=> {
        if (!valid) {
          return;
        }
        let url = "/manager/add" ;
        console.log(this.addManagerForm)
        this.addManagerForm.roleIds = this.roleIds ;
        this.$axios.post(url,this.addManagerForm)
            .then(res=>{
              if(res.data.code == 200){
                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })
                this.addDialogVisible = false;
                this.initManager();
              }else {
                this.$message({
                  type:'error',
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      })

    },
    //关系管理员的对话框
    addManagerFormClose() {
      this.addDialogVisible = false ;
      this.$refs.addManagerFormRef.resetFields();
      this.roleIds = [];
    },
    //取消添加管理员
    cancelAdd(){
      this.addDialogVisible = false ;
      this.$refs.addManagerFormRef.resetFields();
      this.roleIds = [];
    },



    //--------------------------------------------------------初始化查询 所有角色----------------------------------------------
    initRole(){
      let url = "/role/showAllRole" ;
      this.$axios.get(url).then(res=>{
        if(res.data.code == 200){
          this.roleList = res.data.data ;
          console.log("0000000000000000000000000")
          console.log(this.roleList)
          console.log("0000000000000000000000000")
        }else {
          this.$message({
            type:'error',
            message:res.data.msg,
            duration:1000
          })
        }
      })
    },


    //----------------------------------------------------------修改管理员---------------------------------------------------
    showEditDialog(row){
      let arr=[];
      for (arr of row.roles){
        this.roleIds.push(arr.id);
      }
      this.editDialogVisible = true;
      this.editManagerForm = row;
    },
    editManagerFormClose(){
      this.$refs.editManagerFormRef.resetFields();
      this.roleIds = [];
      this.editManagerForm.deptId = "";
      this.editManagerForm.roleIds = "";
    },
    editManager(){
      this.$refs.editManagerFormRef.validate(valid=> {
        if (!valid) {
          return;
        }
        let url = "/manager/editManager";
        console.log(this.editManagerForm)
        this.editManagerForm.roleIds = this.roleIds;
        this.$axios.put(url, this.editManagerForm)
            .then(res => {
              if (res.data.code == 200) {
                this.$message({
                  type: "success",
                  message: res.data.msg,
                  duration: 1000
                })
                this.editDialogVisible = false;
                this.initManager();
              } else {
                this.$message({
                  type: 'error',
                  message: res.data.msg,
                  duration: 1000
                })
              }
            })
      })
    },




    // ------------------------------------------------------删除管理员信息------------------------------------------------
    removeManagerById(id) {
      this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let url = "/manager/del/"+id ;
        this.$axios.delete(url)
            .then(res=>{
              if (res.data.code == 200){
                this.$message({
                  type:"success",
                  message:res.data.msg,
                  duration:1000
                })
                this.initManager();
              }else {
                this.$message({
                  type:"error",
                  message:res.data.msg,
                  duration:1000
                })
              }
            })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
  },
  created() {
    this.initManager();
    this.initRole();
  }
}
</script>

<style scoped>

</style>
